package com.corpgovernment.travelstandard.loader;

import com.corpgovernment.travelstandard.entity.db.MbTravelstandReasonCode;
import com.corpgovernment.travelstandard.mapper.MbTravelstandReasonCodeMapper;
import com.ctrip.corp.obt.generic.utils.CollectionUtils;
import com.ctrip.corp.obt.generic.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author jhxue
 * @create 2022-05-12-18:50
 */
@Component
public class MbTravelStandardReasonCodeLoader {

	@Autowired
	private MbTravelstandReasonCodeMapper travelstandReasonCodeMapper;

	/**
	 * 根据tsId查询
	 *
	 * @param tsId
	 * @return
	 */
	public List<MbTravelstandReasonCode> selectByTsId(Long tsId) {
		if (tsId == null) {
			return new ArrayList<>();
		}
		MbTravelstandReasonCode record = new MbTravelstandReasonCode();
		record.setTsId(tsId);
		return travelstandReasonCodeMapper.select(record);
	}

	/**
	 * 根据tsIds查询
	 *
	 * @param tsIds
	 * @return
	 */
	public List<MbTravelstandReasonCode> selectByTsIds(List<Long> tsIds, String tsType) {
		if (CollectionUtils.isEmpty(tsIds)) {
			return new ArrayList<>();
		}
		Example example = new Example(MbTravelstandReasonCode.class);
		example.createCriteria().andIn("tsId", tsIds).andEqualTo("tsType", tsType);
		return travelstandReasonCodeMapper.selectByExample(example);
	}

	public boolean batchSave(List<MbTravelstandReasonCode> rcList) {
		if (CollectionUtils.isEmpty(rcList)) {
			return false;
		}
		int count = (int) rcList.stream().map(travelstandReasonCodeMapper::insertSelective).count();
		return count > 0;
	}

	public boolean deleteByTsIdAndType(Long tsId, String tsType) {
		if (tsId == null || StringUtils.isEmpty(tsType)) {
			return false;
		}
		Example example = new Example(MbTravelstandReasonCode.class);
		example.createCriteria()
			   .andEqualTo("tsId", tsId)
			   .andEqualTo("tsType", tsType);
		return travelstandReasonCodeMapper.deleteByExample(example) > 0;
	}

	public List<MbTravelstandReasonCode> selectByTsIdAndType(Long tsId, String tsType) {
		if (tsId == null || StringUtils.isEmpty(tsType)) {
			return Collections.emptyList();
		}
		Example example = new Example(MbTravelstandReasonCode.class);
		example.createCriteria()
			   .andEqualTo("tsId", tsId)
			   .andEqualTo("tsType", tsType);
		return travelstandReasonCodeMapper.selectByExample(example);
	}

	public boolean deleteByTsIdAndRcType(Long tsId, String rcType) {
		if (tsId == null || StringUtils.isEmpty(rcType)) {
			return false;
		}
		Example example = new Example(MbTravelstandReasonCode.class);
		example.createCriteria()
			   .andEqualTo("tsId", tsId)
			   .andEqualTo("tsRcType", rcType);
		return travelstandReasonCodeMapper.deleteByExample(example) > 0;
	}
}
